/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgApp;
import com.iecg.service.IecgAppService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * app管理
 *
 * @author pigx code generator
 * @date 2020-01-07 15:50:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/appmanage" )
@Api(value = "iecgapp", tags = "管理app")
public class IecgAppManageController {

    private final IecgAppService iecgAppService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param iecgApp app管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getIecgAppPage(Page page, IecgApp iecgApp) {

		QueryWrapper<IecgApp> queryWrapper = new QueryWrapper();
		if(iecgApp.getAppVersion() != null){
			queryWrapper.eq("app_version", iecgApp.getAppVersion());
		}
		if(iecgApp.getDeviceId() != null){
			queryWrapper.eq("device_id", iecgApp.getDeviceId());
			return R.ok(iecgAppService.page(page, Wrappers.query(iecgApp)));
		}
		if(iecgApp.getLoginName() !=null){
			queryWrapper.eq("login_name", iecgApp.getLoginName());
		}
		System.out.println(Wrappers.query(iecgApp).getCustomSqlSegment());
		return R.ok(iecgAppService.getPageTestList(page, queryWrapper));
    }


    /**
     * 通过id查询app管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iecgAppService.getById(id));
    }

    /**
     * 新增app管理
     * @param iecgApp app管理
     * @return R
     */
    @ApiOperation(value = "新增app管理", notes = "新增app管理")
    @SysLog("新增app管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iecg_appmanage_add')" )
    public R save(@RequestBody IecgApp iecgApp) {
        return R.ok(iecgAppService.save(iecgApp));
    }

    /**
     * 修改app管理
     * @param iecgApp app管理
     * @return R
     */
    @ApiOperation(value = "修改app管理", notes = "修改app管理")
    @SysLog("修改app管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iecg_appmanage_edit')" )
    public R updateById(@RequestBody IecgApp iecgApp) {
        return R.ok(iecgAppService.updateById(iecgApp));
    }

    /**
     * 通过id删除app管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除app管理", notes = "通过id删除app管理")
    @SysLog("通过id删除app管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iecg_appmanage_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(iecgAppService.removeById(id));
    }

}
